package cn.edu.zua.demo06.module.test.controller;


import cn.edu.zua.demo06.module.test.entity.User;
import cn.edu.zua.demo06.module.test.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import cn.edu.zua.demo06.base.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author ascend
 * @since 2019-01-23
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    private UserMapper userMapper;

    @Autowired
    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping("/login")
    public String login(User user, HttpServletRequest request) {
        log.debug("用户信息:{}", user);
        String resultMsg = "登录成功";
        User result = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", user.getUserName()).eq("user_password", user.getUserPassword()));
        if (Objects.isNull(result)) {
            log.debug("没有找到该用户");
            resultMsg = "用户名或密码错误";
        }else {
            log.debug("找到了该用户");
            request.getSession().setAttribute("_session_user", user);
        }

        return resultMsg;
    }

}
